package com.zsl.dao.impl;

import com.zsl.dao.OrderInfoDao;
import com.zsl.model.OrderInfo;
import com.zsl.util.C3P0Util;
import com.zsl.util.DbHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderInfoDaoImpl implements OrderInfoDao {
    @Override
    public String getOrderInfoCount(String[] whereColumn, String[] whereValue) throws SQLException {
        StringBuilder sql = null;
        String count = null;
        //只返回数量
        sql = new StringBuilder("SELECT count(*) FROM orderinfo,users WHERE orderinfo.user_id=users.id ");
        if (whereColumn != null) {
            for (String wc : whereColumn) {
                sql.append(" AND ").append(wc).append(" =?");
            }
        }
        ResultSet rs = DbHelper.executeQuery(sql.toString(), whereValue);
        while (rs.next()) {
            count = rs.getString(1);
        }
        //关闭连接
        C3P0Util.release(DbHelper.getPs(), DbHelper.getConn(), rs);
        return count;
    }

    @Override
    public List<OrderInfo> getOrderInfo(String[] whereColumn, String[] whereValue) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        StringBuilder sql = null;
        List<OrderInfo> orderInfos = new ArrayList<>();

        sql = new StringBuilder("select * from orderinfo,users where orderinfo.user_id=users.id");
        if (whereColumn != null) {
            for (String wc : whereColumn) {
                sql.append(" AND ").append(wc).append(" =?");
            }
        }
        ResultSet rs = DbHelper.executeQuery(sql.toString(), whereValue);
        while (rs.next()) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setId(rs.getString("orderinfo.id"));
            orderInfo.setOrderId(rs.getString("orderId"));
            orderInfo.setNum(rs.getInt("num"));
            orderInfo.setPrice(rs.getFloat("price"));
            orderInfo.setStatus(rs.getString("status"));
            orderInfo.setUser_id(rs.getString("user_id"));
            orderInfo.setGenerate_user_name(rs.getString("username"));
            orderInfos.add(orderInfo);
        }
        //关闭连接
        C3P0Util.release(DbHelper.getPs(), DbHelper.getConn(), rs);
        return orderInfos;
    }

    @Override
    public Map<String, Object> insertOrderInfo(List<OrderInfo> orderInfos) {
        String sql = "INSERT INTO orderinfo (id,orderId,num,price,status,user_id,is_deleted) VALUES(?,?,'0',0,'0',?,'0')";
        List<Integer> line = new ArrayList<>();
        List<String> orderInfo_id = new ArrayList<>();
        for (OrderInfo orderInfo : orderInfos) {
            line.add(DbHelper.executeUpdate(sql, new Object[]{orderInfo.getId(), orderInfo.getOrderId(), orderInfo.getUser_id()}));
            orderInfo_id.add(orderInfo.getId());
        }
        Map<String, Object> result = new HashMap<>();
        result.put("line", line);
        result.put("orderInfo_id", orderInfo_id);
        return result;
    }

    @Override
    public Map<String, Object> updateOrderInfoById(List<OrderInfo> orderInfos, int selectModel) {
        Map<String, Object> result = new HashMap<>();
        List<Integer> line = new ArrayList<>();
        List<String> orderInfo_id = new ArrayList<>();
        //update num,price
        if (selectModel == 1) {
            for (OrderInfo orderInfo : orderInfos) {
                orderInfo_id.add(orderInfo.getId());
                line.add(DbHelper.update("orderinfo", new String[]{"num", "price"},
                        new String[]{orderInfo.getNum().toString(), orderInfo.getPrice().toString()},
                        new String[]{"id"},
                        new String[]{orderInfo.getId()}));
            }
            result.put("line", line);
            result.put("orderInfo_id", orderInfo_id);
        }
        //update status(更新订单状态
        else if(selectModel==2){
            for (OrderInfo orderInfo : orderInfos) {
                orderInfo_id.add(orderInfo.getId());
                line.add(DbHelper.update("orderinfo", new String[]{"status"},
                        new String[]{orderInfo.getStatus()},
                        new String[]{"id"},
                        new String[]{orderInfo.getId()}));
            }
            result.put("line", line);
            result.put("orderInfo_id", orderInfo_id);
        }
        return result;
    }
}
